package kcms.assignment.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import kcms.assignment.dao.AssignmentDao;
import kcms.db.Assign;
import kcms.db.Assignment;
import kcms.db.Assignmentcontent;
import kcms.db.Assignmentscore;
@Repository
public class AssignmentDaoImpl extends HibernateDaoSupport implements AssignmentDao {
	
	@Resource
	private SessionFactory sessionFactory;
	@Resource
	private Assignmentcontent asc;
	@Resource
	private Assignment a;
	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	public boolean saveNewTask(Assignment as){
		try{
		if(((List<Assignment>)this.getHibernateTemplate().find("from Assignment a where a.cid=? and a.csn=? and a.assignmentname=?",as.getCid(),as.getCsn(),as.getAssignmentname())).isEmpty()){
			this.getHibernateTemplate().save(as);
			return true;
		}else
			return false;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Assign> getAllAssignBySid(String id){
		return (List<Assign>) this.getHibernateTemplate().find("from Assign a where a.sid=?", id);
	}
	@SuppressWarnings("unchecked")
	public Assign getAssignByAid(String aid){
		List<Assign> temp=(List<Assign>) this.getHibernateTemplate().find("from Assign a where a.assignmentid='"+aid+"'");
		if (temp.isEmpty())
			return null;
		else
			return temp.get(0);
	}
	@SuppressWarnings("unchecked")
	public Assignmentcontent getAssignmentcontentByAid(Integer assignmentid){
		List<Assignmentcontent> temp=(List<Assignmentcontent>) this.getHibernateTemplate().find("from Assignmentcontent a where a.assignmentid=?", assignmentid);
		if(temp.isEmpty())
			return null;
		else
			return temp.get(0);
	}
	public boolean deleteAssignmentcontent(Assignmentcontent asc){
		try{
		this.getHibernateTemplate().delete(asc);
		return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public boolean deleteAssign(String assignmentsn){
		try{
			if(this.getHibernateTemplate().find("from Assignment a where a.assignmentsn='"+assignmentsn+"'")!=null){
				a=(Assignment) this.getHibernateTemplate().find("from Assignment a where a.assignmentsn='"+assignmentsn+"'").get(0);
				this.getHibernateTemplate().delete(a);
				return true;
			}else
				return false;
			
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public boolean saveContentRecord(Integer assignmentid, String docFileName,
			String filename){
		try{
		asc.setAssignmentid(assignmentid);
		asc.setName(docFileName);
		asc.setResource(filename);
		asc.setUploadtime(new Date());
		this.getHibernateTemplate().save(asc);
		return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Assign> getAllAssignByCidCsn(String cid, String csn){
		return (List<Assign>) this.getHibernateTemplate().find("from Assign a where a.cid=? and a.csn=?", cid,csn);
	}
	
	@SuppressWarnings("unchecked")
	public List<Assign> getAllAssignByCidCsnAsn(String cid, String csn,
			String assignmentsn){
		return (List<Assign>) this.getHibernateTemplate().find("from Assign a where a.cid=? and a.csn=? and a.assignmentsn='"+assignmentsn+"'",cid,csn);
	}
	
	@SuppressWarnings("unchecked")
	public List<Assign> getAllAssignByAsn(String assignmentsn){
		return (List<Assign>) this.getHibernateTemplate().find("from Assign a where a.assignmentsn='"+assignmentsn+"'");
	}
	@SuppressWarnings("unchecked")
	public Assign getAssignBySidAsn(String sid, String asn){
		List<Assign> temp=(List<Assign>) this.getHibernateTemplate().find("from Assign a where a.sid=? and a.assignmentsn='"+asn+"'",sid);
		if(temp.isEmpty())
			return null;
		else
			return temp.get(0);
	}
	
	public boolean saveNewScore(Assignmentscore assignmentscore){
		try{
			this.getHibernateTemplate().saveOrUpdate(assignmentscore);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public Assignmentcontent getAsc() {
		return asc;
	}

	public void setAsc(Assignmentcontent asc) {
		this.asc = asc;
	}

	public Assignment getA() {
		return a;
	}

	public void setA(Assignment a) {
		this.a = a;
	}
}
